#pragma once
#include<iostream>
#include<string>
#include<sstream>
#include<memory>
#include<jsoncpp/json/json.h>
// {json} -> len\r\n{json}\r\n

const std::string token = "\r\n";//分隔符

bool Encode(std::string &message)//封装报文
{
    if(message.size()==0) return false;
    std::string package = std::to_string(message.size()) + token + message + token;
    message = package;
    return true;
}

bool Decode(std::string &package,std::string *content)//解码报文
{
    auto pos = package.find(token);
    if(pos == std::string::npos) return false;
    std::string content_len_str = package.substr(0,pos);//截取报文内容
    int content_len = std::stoi(content_len_str);//截取报头长度，方便后续解码
    int full_len = content_len + 2*token.size() + content_len_str.size();
    if(full_len > package.size()) return false;
    *content = package.substr(pos+token.size(),content_len);//截取报文内容
    package.erase(0,full_len);//处理掉已解码的报文
    return true;
}

class Request{
    public:
        Request() = default;
        Request(int x , int y , char op)
            :_x(x),_y(y),_op(op)
        {}
        bool Serialize(std::string &out_string)//序列化
        {
            Json::Value root;
            root["x"] = _x;
            root["y"] = _y;
            root["op"] = _op;
            Json::StreamWriterBuilder builder;//构建流写入器
            std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
            std::stringstream ss;
            writer->write(root, &ss);//写入json流
            out_string = ss.str();
            return true;
        }
        bool Deserialize(std::string &in_string)//反序列化
        {
            Json::Value root;
            Json::Reader reader;
            if(!reader.parse(in_string,root)) {
                std::cout<<"parse json failed"<<reader.getFormattedErrorMessages()<<std::endl;
                return false;//解析json失败
            }
            _x = root["x"].asInt();
            _y = root["y"].asInt();
            _op = root["op"].asInt();
            return true;
        }
        int GetX() const {return _x;}
        int GetY() const {return _y;}
        char GetOp() const {return _op;}
    private:
        int _x;
        int _y;
        char _op;
};

class Response{
    public:
        Response() = default;
        Response(int result, int code)
            :_result(result),_code(code)
        {}
        bool Serialize(std::string &out_string)//序列化
        {
            Json::Value root;
            root["result"] = _result;
            root["code"] = _code;
            Json::StreamWriterBuilder builder;
            std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
            std::stringstream ss;
            writer->write(root, &ss);
            out_string = ss.str();
            return true;
        }
        bool Deserialize(std::string &in_string)//反序列化
        {
            Json::Value root;
            Json::Reader reader;
            if(!reader.parse(in_string,root)) {
                std::cout<<"parse json failed"<<reader.getFormattedErrorMessages()<<std::endl;
                return false;
            }
            _result = root["result"].asInt();
            _code = root["code"].asInt();
            return true;
        }
        int GetResult() const {return _result;}
        int GetCode() const {return _code;}
        void SetResult(int result) {_result = result;}
        void SetCode(int code) {_code = code;}
    private:
        int _result;//结果
        int _code = 0;//状态码0,1,2,3
};




